﻿using _1_Interfaces.Interfaces.Cjw;
using _1_Interfaces.Models.Dtos.Cjw;
using _1_Interfaces.Models.Tables.Cjw;
using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace _2_Services.Services.Cjw
{
    /// <summary>
    /// 操作记录表服务
    /// </summary>
    public class OperationListService : IOperationListService
    {
        SqlSugarClient _db = CjwDbContext.GetInstance();
        private IMapper _mapper;

        public OperationListService(IMapper mapper)
        {
            _mapper = mapper;
        }

        /// <summary>
        /// 关于操作记录插入
        /// </summary>
        /// <param name="operationListAddDto"></param>
        public void InsertOpData(OperationListAddDto operationListAddDto)
        {
            OperationList operationList = _mapper.Map<OperationList>(operationListAddDto);
            operationList.OpTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            _db.Insertable(operationList).ExecuteCommand();
        }

        /// <summary>
        /// 获取操作日志
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">一页几条</param>
        /// <returns></returns>
        public OperationListDto GetOpList(string startTime, string endTime, int pageIndex, int pageSize)
        {
            OperationListDto operationListDto = new OperationListDto();
            int totalCount = 0;
            operationListDto.OperationLists = _db.Queryable<OperationList>()
                .WhereIF(startTime != null, x => Convert.ToDateTime(x.OpTime) >= Convert.ToDateTime(startTime))
                .WhereIF(endTime != null, x => Convert.ToDateTime(x.OpTime) < Convert.ToDateTime(endTime).AddDays(1))
                .OrderBy(x => x.OpId, OrderByType.Desc)
                .ToPageList(pageIndex, pageSize, ref totalCount);
            operationListDto.Total = totalCount;
            operationListDto.TotalPage = Tool.PageCount(totalCount, pageSize);
            return operationListDto;
        }
    }
}
